import api from "./api.js"
class Base {
  constructor() {}
  login() {
    var that = this
    return new Promise((resolve, reject) => {
      wx.showToast({
        title: '登录中...',
        icon: "loading"
      })
      wx.login({
        success: (res) => {
          if (res.code) {
            api.login(res.code).then(res => {
              wx.hideToast();
              if (res.status == 1) {
                if (res.result.token) {
                  wx.setStorageSync('token', res.result.token)
                  return resolve()
                }
              } else if (res.status == 3) {
                that.globalData.token = res.result.token
                wx.reLaunch({
                  url: '/pages/login/loginStepOne/loginStepOne',
                })
              } else {
                console.log(res.msg)
              }
            })
          }
        },
        fail: (res) => {
          console.log("网络连接异常2", res)
          Modal.errModal("网络连接异常")
        }
      })
    })
  }

}
//------------------------模态框
class Modal {
  constructor() {}
  /**
   * failToast
   */
  failToast(title) {
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 1500
    })
  }
  /**
   * successToast
   */
  successToast(title) {
    wx.showToast({
      title: title,
      icon: 'success',
      duration: 1000
    })
  }
  /**
   * 单确定提示框
   */
  sureModal(content, callback) {
    wx.showModal({
      title: '温馨提示',
      content: content,
      showCancel: false,
      success: function(res) {
        if (res.confirm && callback) {
          callback();
        }
      }
    })
  }
  /**
   * 错误提示框
   */
  errModal(content, callback) {
    wx.showModal({
      title: '错误提示',
      content: content,
      showCancel: false,
      success: function(res) {
        if (res.confirm && callback) {
          callback();
        }
      }
    })
  }
  /**
   * 选择提示框
   */
  selectModal(title, content) {
    return new Promise((resolve, reject) => {
      wx.showModal({
        title: title,
        content: content,
        showCancel: true,
        success: res => {
          resolve(res)
        },
        fail: res => {
          reject(res)
        }
      })
    })
  }
  /**
   * 单选提示框
   */
  sureSelectModal(title, content) {
    return new Promise((resolve, reject) => {
      wx.showModal({
        title: title,
        content: content,
        showCancel: false,
        success: res => {
          resolve(res)
        },
        fail: res => {
          reject(res)
        }
      })
    })
  }
}

//-----------------------正则相关
class Regular {
  constructor() {

  }
  //匹配手机号

  phone(data) {
    return /^1[3|4|5|7|8][0-9]{9}$/.test(data)
  }

  idcard(data) {
    return /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(data)
  }
}
//-------------------文件系列------------------------//
class File {
  constructor() {

  }
  downFile(url) {
    return new Promise((resolve, reject) => {
      wx.downloadFile({
        url: url,
        success: res => {
          resolve(res)
        },
        fail: res => {
          reject(res)
        }
      })
    })
  }
  saveFile(tempFilePath) {
    return new Promise((resolve, reject) => {
      wx.saveFile({
        tempFilePath: tempFilePath,
        success: res => {
          resolve(res)
        },
        fail: res => {
          reject(res)
        }
      })
    })
  }
  openFile(filePath) {
    return new Promise((resolve, reject) => {
      wx.openDocument({
        filePath: filePath,
        success: res => {
          resolve(res)
        },
        fail: res => {
          reject(res)
        }
      })
    })
  }
  getSavedFileList() {
    return new Promise((resolve, reject) => {
      wx.getSavedFileList({
        success: res => {
          resolve(res)
        },
        fail: res => {
          reject(res)
        }
      })
    })
  }
}
class Time {
  constructor() {

  }
  time1(data) {
    var month = data.substr(5, 2);
    var day = data.substr(8, 2);
    var hour = data.substr(11, 2);
    var mintus = data.substr(14, 2);
    return month + '月' + day + '日 ' + hour + ':' + mintus
  }
}
export {
  Base,
  Modal,
  Regular,
  File,
  Time
}